package com.zhoujinlong.ui.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.core.adapter.RecyclerAdapter;
import com.android.core.ui.BaseFragment;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.zhoujinlong.R;
import com.zhoujinlong.model.bean.ExerciseBean;
import com.zhoujinlong.ui.activity.DetailActivity;
import com.zhoujinlong.ui.adapter.ExerciseRecyclerAdapter;
import com.zhoujinlong.ui.widget.RecycleViewDivider;
import com.zhoujinlong.ui.widget.TitleBar;
import com.zhoujinlong.utils.BaseUtil;
import com.zhoujinlong.utils.JsonUtils;
import com.zhoujinlong.utils.ResourceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 会写代码的小新 on 2016/3/31.
 */
public class GuideFragment extends BaseFragment implements RecyclerAdapter.OnItemClickListener {


    @Bind(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @Bind(R.id.title_bar)
    TitleBar mTitleBar;

    private ExerciseRecyclerAdapter mAdapter;
    private List<ExerciseBean> guides = new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_guide;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void onInitView() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mTitleBar.setBackFinishEnable(false);
        mTitleBar.setTitle(R.string.tab_text_guide);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerview.addItemDecoration(new RecycleViewDivider(
                getActivity(), LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.colorPrimary)));
        mAdapter = new ExerciseRecyclerAdapter(getActivity(), R.layout.item_guide_list, guides);
        mRecyclerview.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        getData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getData() {
        new DataTask().execute(BaseUtil.getFileName("guide"));
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, Object o, int position) {
        DetailActivity.start(getActivity(), (ExerciseBean) o);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
        return false;
    }

    private class DataTask extends AsyncTask<String, Integer, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        //doInBackground方法内部执行后台任务,不可在此方法内修改UI
        @Override
        protected String doInBackground(String... params) {
            String data = "";
            try {
                data = ResourceUtils.geFileFromAssets(getActivity(), params[0]);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return data;
        }

        //onProgressUpdate方法用于更新进度信息
        @Override
        protected void onProgressUpdate(Integer... progresses) {

        }

        //onPostExecute方法用于在执行完后台任务后更新UI,显示结果
        @Override
        protected void onPostExecute(String result) {
            if (!TextUtils.isEmpty(result)) {
                List<ExerciseBean> planBeanList = null;
                try {
                    planBeanList = JsonUtils.deserialize(result, new TypeToken<List<ExerciseBean>>() {
                    }.getType());
                    guides.addAll(planBeanList);
                    mAdapter.notifyDataSetChanged();
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }
        }

        //onCancelled方法用于在取消执行中的任务时更改UI
        @Override
        protected void onCancelled() {

        }
    }
}
